package com.yc.demo.d0303;

import com.google.gson.Gson;
import com.yc.demo.bean.Result;
import com.yc.demo.bean.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "LoginServlet", value = "/login.s")
public class LoginServlet extends HttpServlet {
    public  static  User user;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Result result = new Result();

        //验证用户是否输入正确
        String ucode = request.getParameter("vcode");
        Object vcode = request.getSession().getAttribute("vcode");
        if (vcode.equals(ucode)==false){
            result.setMsg("验证码错误!");
        }else {
            String name = request.getParameter("name");
            String pwd = request.getParameter("pwd");
            if (("yc".equals(name) ||"zs".equals(name) ||"ls".equals(name)) && "123".equals(pwd)){
                result.setMsg("登录成功！");
                user=new User();
                request.getSession().setAttribute("loginedUser",name);
                request.getSession().setAttribute("starttime",new Date().getTime());
                Object requestURI = request.getSession().getAttribute("requestURI");
                System.out.println("Login requestURI = " + requestURI);
                result.setData(requestURI);
            }else {
                result.setMsg("用户名或密码错误！");
            }
        }
         String json= new Gson().toJson(result);
        response.getWriter().append(json);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
